package cn.edu.cug.cs.gtl.renders.openinventor.callbacks;

import cn.edu.cug.cs.gtl.apis.render.Selector;
import cn.edu.cug.cs.gtl.apis.render.nodes.Node;
import cn.edu.cug.cs.gtl.renders.openinventor.utils.SelectorImpl;
import com.openinventor.inventor.SbColor;
import com.openinventor.inventor.SoPath;
import com.openinventor.inventor.misc.callbacks.SoSelectionPathCB;
import com.openinventor.inventor.nodes.*;

import java.awt.*;

public class MyDeselectionPathCallback extends SoSelectionPathCB {

    Selector selector=null;


    public MyDeselectionPathCallback(Selector s){
        this.selector = s;
    }
    @Override
    public void invoke(SoPath p ) {

        SoNode t = p.regular.getTail();

        if (t instanceof SoIndexedFaceSet
                || t instanceof SoIndexedLineSet
                || t instanceof SoIndexedPointSet) {

            int l = p.regular.getLength();
            if(l>=2) {
                SoNode n = p.regular.getNodeFromTail(1);
                if (n instanceof SoSeparator) {
                    String nodeName = n.getName();
                    Node node = selector.remove(nodeName);
                    for(int i=0;i<((SoSeparator) n).getNumChildren();++i){
                        SoNode n1 = ((SoSeparator) n).getChild(i);
                        if (n1 instanceof SoMaterial) {
                            ((SoMaterial) n1).diffuseColor.setValues(0,(SbColor[]) node.getUserData());
                            break;
                        }
                    }
                }
            }
        }
    }


}
